package com.hsy.linemoney.modules.my.mycustomer;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.deparse.dpframe.tools.utils.DPUnitUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.apiservice.AgentService;
import com.hsy.linemoney.apiservice.Callback;
import com.hsy.linemoney.apiservice.LineMoneyContext;
import com.hsy.linemoney.beans.User;
import com.hsy.linemoney.modules.common.CommonBaseActivityWithSimpleActionBar;
import com.hsy.linemoney.utils.CommonUtils;
import com.hsy.linemoney.utils.NumberUtil;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 我所属机构的点数汇总
 * Created by Administrator on 2016/7/1.
 */
public class MyCustomerActivity extends CommonBaseActivityWithSimpleActionBar {
    private ListView listView;
    private List<MyCustomerBean> listData;
    private LayoutInflater inflater;
    private MyCustomerAdapter adapter;

    private View listHeader, listFooter;

    private TextView headerLabel;
    private TextView headerValue;

    private TextView footerLabel;
    private TextView footerValue;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        listView = new ListView(this);
        setContentView(listView);
        setTitle(R.string.txt_my_team_gain);

        listView.setBackgroundColor(Color.parseColor("#f8f8f8"));
        listView.setDivider(new ColorDrawable(Color.parseColor("#f8f8f8")));
        listView.setDividerHeight(DPUnitUtil.dip2px(this, 15));
        listView.setSelector(new ColorDrawable(Color.TRANSPARENT));

        inflater = LayoutInflater.from(this);
        listHeader = inflate(R.layout.my_custom_list_header);
        listFooter = inflate(R.layout.my_custom_list_footer);
        listView.addHeaderView(listHeader);
        listView.addFooterView(listFooter);

        headerLabel = (TextView) listHeader.findViewById(R.id.header_label);
        headerValue = (TextView) listHeader.findViewById(R.id.header_value);

        footerLabel = (TextView) listFooter.findViewById(R.id.footer_label);
        footerValue = (TextView) listFooter.findViewById(R.id.footer_value);

        showProgressDialog(this);
        final AgentService service = new AgentService();
        service.getPointRollUpRatio(new Callback<AgentService.PointRollUpRatioResult>() {
            @Override
            public void onSuccess(int what, final AgentService.PointRollUpRatioResult sPointRollUpRatioResult) {
                if (null == listData) {
                    listData = new ArrayList<MyCustomerBean>() {
                        @Override
                        public boolean add(MyCustomerBean object) {
                            if (0 == size()) {
                                super.add(object);
                            } else if (object.getLevel() < get(0).getLevel()) {
                                super.add(0, object);
                            } else if (object.getLevel() > get(size() - 1).getLevel()) {
                                super.add(object);
                            } else {
                                for (int i = 0; i < size() - 2; i++) {
                                    if (get(i).getLevel() < object.getLevel() && get(i + 1).getLevel() >= object.getLevel()) {
                                        super.add(i, object);
                                    }
                                }
                            }
                            return true;
                        }
                    };
                }
                for (AgentService.PointRollUpRatioResult.Level item : sPointRollUpRatioResult.getLevels()) {
                    MyCustomerBean bean = new MyCustomerBean();
                    bean.setLevel(Integer.parseInt(item.getLevel()));
                    bean.setRatio(item.getRatio());
                    listData.add(bean);
                }
                service.getOrgPointForAllLevel(new Callback<AgentService.OrgPointForAllLevelResult>() {
                    @Override
                    public void onSuccess(int what, AgentService.OrgPointForAllLevelResult sOrgPointForAllLevelResult) {
                        for (AgentService.OrgPointForAllLevelResult.Value item : sOrgPointForAllLevelResult.getValue()) {
                            for (int i = 0; i < listData.size(); i++) {
                                if (listData.get(i).getLevel() == item.getLevel()) {
                                    listData.get(i).setPoints(item.getPoints());
                                    break;
                                }
                            }
                        }
                        User user = LineMoneyContext.getLoggedUser();
                        headerValue.setText(user.getUserId() + "-" + user.getFirstName() + "-Team");

                        double re = 0;
                        for (MyCustomerBean item : listData) {
                            re += item.getPoints() * item.getRatio();
                        }
                        footerLabel.setText(getString(R.string.txt_x_layer_total_points, NumberUtil.numToString(listData.size())));
                        footerValue.setText(String.format(LineMoneyContext.getLocalLanguagePair().locale, "%.2f", re));

                        adapter = new MyCustomerAdapter(MyCustomerActivity.this, listData);
                        listView.setAdapter(adapter);
                        hideProgressDialog();
                    }

                    @Override
                    public void onError(int what, int type, String msg, Exception e) {
                        hideProgressDialog();
                    }
                });
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                hideProgressDialog();
            }
        });
    }
}
